//
//  LCSegmentdeControl.m
//  01 - 自定制segmentedControl
//
//  Created by 千锋 on 16/4/12.
//  Copyright (c) 2016年 千锋. All rights reserved.
//

#import "LCSegmentdeControl.h"
#define BUTTON_TAG 100

@interface  LCSegmentdeControl()

//每个分段的title
@property (nonatomic,strong)NSArray *items;

@end


@implementation LCSegmentdeControl{
    //响应消息的对象
    id _target;
    //消息
    SEL _action;
    
    //滑块
    UIView   *_sliderView;
}




//添加事件
- (void)addTarget:(id)target action:(SEL)action{
    _target = target;
    _action = action;
}




- (instancetype)initWithItems:(NSArray *)array{
    if (self = [ super init]) {
        //给items属性赋值
        _items  =  array;
        
        //===========做一些初始化工作===============
        //默认的选中颜色
        _selectedColor = [UIColor blueColor];
    }
    return  self;
}
//重写选中下标
- (void)setSelectedSegmentIndex:(NSUInteger)selectedSegmentIndex{
    
    _selectedSegmentIndex = selectedSegmentIndex;

    
    //将选中下标对应的按钮变成选中状态
    UIButton *btn = (UIButton *)[self viewWithTag:_selectedSegmentIndex + BUTTON_TAG ];
    //设置选中状态
    btn.selected = YES;
    btn.userInteractionEnabled = NO;
    
    //改变滑块的中心店
    [UIView animateWithDuration:1 animations:^{
        _sliderView.center = CGPointMake(btn.center.x, self.frame.size.height - _sliderView.frame.size.height/2.0f);

    }];
}

//重写items的set方法
- (void)setItems:(NSArray *)items{
    _items = items;
    _selectedColor = [UIColor redColor];
    
}

//通过items去创建对应的按钮
- (void)createButton{
    //为了减轻cpu压力，放外面算，省的每次循环算
    CGFloat W = self.frame.size.width / _items.count;
    CGFloat H = self.frame.size.height;
    CGFloat Y = 0;
    
    
    
    //遍历创建对应的按钮
    int i = 0;
    for (NSString *title in _items) {
        CGFloat X = i * W;
        
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(X, Y, W, H)];
        //设置按钮文字
        [button setTitle:title forState:UIControlStateNormal];
        
        
        
        //设置按钮文字颜色
        [button setTitleColor:_normalColor forState:UIControlStateNormal];
        
        [button setTitleColor:_selectedColor forState:UIControlStateSelected];
        [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown];
        
        //设置tag(不能从0开始)
        button.tag = i + BUTTON_TAG;
        
        [self addSubview:button];
        
        i++;
    }
    //======================创建滑条=======================
    CGFloat SW = W * 0.4;
    CGFloat SH = 5;
    _sliderView = [[UIView alloc]initWithFrame:CGRectMake(0, H - 5, SW, SH)];
    _sliderView.backgroundColor = _selectedColor;
    //[self addSubview:_sliderView];
    
    //================创建底部的线=====================
    
    CGFloat lh = 1.5;
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, H - lh, self.frame.size.width, lh)];
    line.backgroundColor = _selectedColor;
  //  [self addSubview:line];
}

- (void)onClick:(UIButton *)button{
    
    //通过选中下标拿到当前选中的按钮
    UIButton *selectedBtn =(UIButton *)[self viewWithTag:_selectedSegmentIndex + BUTTON_TAG ];
    //将之前选中的变成非选中状态
    selectedBtn.selected = NO;
    //让按钮可以点击
    selectedBtn.userInteractionEnabled = YES;
    
    
//    //让被点击的按钮变成选中状态
//    button.selected = YES;
//    //让按钮不可点击
//    button.userInteractionEnabled = NO;
//    //更新当前下标
//    _selectedSegmentIndex  = button.tag - BUTTON_TAG;

    
    //代替上面三句话(因为设置按钮的选中和关闭按钮交互已经重新设置选中下标都已经在_selectedSegmentIndex的set方法中完成了)
    self.selectedSegmentIndex = button.tag - BUTTON_TAG;
    
    
    //3.选中下标的值发生改变,让响应消息的对象去响应消息
    //添加事件
    //系统本来的如果没有响应事件会崩溃，自己做的话，可以防止崩溃
    if ([_target respondsToSelector:_action]) {
    [_target performSelector:_action withObject:self];
    }
    else{
        NSLog(@"警告:事件方法没有实现");
    }
}


//这个方法在视图将要显示在界面上的时候才会调用
//在这个方法中拿到的所有的属性的值，肯定是最终的属性值
//一般在这个方法中去设置子视图的frame
- (void)layoutSubviews{
    [self createButton];
    
    //设置选中下标
    self.selectedSegmentIndex = _selectedSegmentIndex;
}


@end
